package c03.thread.api.interrupt;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.LockSupport;

import static util.Sleeper.sleep;

@Slf4j(topic = "c.ThreadInterruptedTest")
public class ThreadInterruptedTest {
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                log.debug("t1 start ...");
                LockSupport.park();
//                boolean interrupted = Thread.interrupted(); // true
                log.debug("执行LockSupport.park()后，打断状态：{}", Thread.currentThread().isInterrupted());
                LockSupport.park(); // 由于打断标记被清除了，线程会在此park住
            }
        });
        t1.start();

        sleep(1);
        t1.interrupt();
        /*
        1. 如果被打断线程正在 sleep,wait,join 会导致被打断的线程抛出 InterruptedException，并清除打断标记 ;
        2. 如果打断的正在运行的线程,则会设置 打断标记 ;
        3. park 的线程被打断,也会设置 打断标记
         */
    }
}
